<?php
namespace App\Http\Controllers\Api\V2\Wallet;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Business\App\Wallet\Wallet;
use App\Http\Controllers\Business\App\PublicBusiness;
use App\Http\Controllers\Business\App\Auth\AppAuth;
use App\Http\Controllers\Business\App\Salesman\SalesmanManagement;
use App\Http\Controllers\Business\App\Statistic\StatisticManagement;
use Log;


class WalletController extends ApiController
{
    //首页
    public function index() {
        $userId = $this->userId;
        $walletResult = Wallet::index($userId);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //充值列表
    public function rechargeList() {
        $userId = $this->userId;
        $walletResult = Wallet::rechargeOrderList($userId);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //消费列表
    public function orderList() {
        $userId = $this->userId;
        if($this->request->has('commentstatus')) {
            $commentStatus = $this->request->get('commentstatus');
        } else {
            $commentStatus = '';
        }
        $walletResult = Wallet::payOrderList($userId, $commentStatus);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //个人轨迹
    public function course() {
        $userId = $this->userId;
        //$userAgent = $this->request->header('user-agent');
        //$platform = PublicBusiness::getPlatformByUserAgent($userAgent);
        $result = Wallet::course($userId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $courseData = $result['data'];
        $data = [
            //'platform' => $platform,
            'avatar' => $courseData['avatar'],
            'nickname' => $courseData['nickname'],
            'sumday' => $courseData['sumday'],
            'sumrechargemoney' => $courseData['sumrechargemoney'],
            'sumordermoney' => $courseData['sumordermoney'],
            'savemoney' => $courseData['savemoney'],
            'createdat' => date('Y-m-d H:i:s', strtotime($courseData['created_at'])),
            'firstrechargetime' => $courseData['firstrechargetime'],
            'firstordertime' => $courseData['firstordertime'],
            'firstshopname' => $courseData['firstshopname'],
        ];
        $param = http_build_query($data);
        $url = env('WEB_URL') . '/mobile/wallet/course?' . $param;
        return redirect()->to($url);
    }
    //新用户领取翻倍券
    public function newUserDoubleCoupon() {

        //  -------ios 解析不出------
        $currentUrl = url()->full();
        $urlArray = explode("?", $currentUrl);
        if(isset($urlArray['1'])) {
            $url = $urlArray['1'];
        } else {
            return view('errors.404');
        }
        parse_str($url, $param);
        $token = isset($param['token'])? $param['token'] : '';
        $tokenArray = explode('?', $token);
        $token = isset($tokenArray['0'])?$tokenArray['0']: $token;
        $userId=AppAuth::tokenGetUserId($token);
        // -----------ios 解析不出来-------------


        //$userId = $this->userId;
        $result = Wallet::getMobileByUserId($userId);
        if($result['status'] == 'failed') {
            return $this->responseError('token失效');
            return view('errors.404');
        }
        $mobile = $result['data'];
        //$encodeUserId = PublicBusiness::encrypt($userId, 'E', '');
        $url =env('WEB_URL') . '/mobile/wallet/newuser/doublecoupon?mobile=' . $mobile ;
        return redirect()->to($url);
    }
    //充值记录详情
    public function rechargeDetail() {
        $userId = $this->userId;
        $orderNumber = $this->request->get('ordernumber');
        $walletResult = Wallet::rechargeDetails($userId, $orderNumber);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //消费记录详情
    public function orderDetail() {
        $userId = $this->userId;
        $orderNumber = $this->request->get('ordernumber');
        $walletResult = Wallet::orderDetails($userId, $orderNumber);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //输入金额获取最大红包金额
    public function getRedEnvelopeMaxAmountBeforeOrder() {
        $userId = $this->userId;
        $money = $this->request->get('money');
        $versionCode = $this->request->get('versioncode');
        if($versionCode >= '15') {
            $shopId = $this->request->get('shopid');
            $result = Wallet::getRedEnvelopeMaxAmountByV15Version($userId, $money, $shopId);
        } else {
            $result = Wallet::getRedEnvelopeMaxAmount($userId, $money);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //获取付款码
    public function getPayCode() {
        $userId = $this->userId;
        $walletResult = Wallet::getPayCodeByUserId($userId);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        $data['feecontent'] = "";
        //$data['feecontent'] = "平台对每笔消费收取 10/笔手续费";
        return $this->responseSuccess($message, $data);
    }
    //获取支付状态
    public function getPayStatus() {
        $userId = $this->userId;
        $walletResult = Wallet::getPayStatus($userId);
        if($walletResult['status'] == 'failed') {
            return $this->responseError($walletResult['message']);
        }
        $message = '获取信息成功';
        $data = $walletResult['data'];
        return $this->responseSuccess($message, $data);
    }
    //准备支付
    public function apiReadyRecharge() {
        $userId = $this->userId;
        $money = $this->request->get('money');
        $payType = $this->request->get('paytype');
        if(!$this->request->has('apiversion')) {
            if ($this->request->has('isdoublecoupon')) {
                $isDoubleCoupon = $this->request->get('isdoublecoupon');
            } else {
                $isDoubleCoupon = '0';
            }
            if(date('Y-m-d') < '2017-03-01') {
                $apiReadyRecharge = Wallet::apiReadyRecharge($userId, $money, $payType, $isDoubleCoupon);//旧
            } else {
                return $this->responseError('请下载最新app');
            }
        } else {
            if($this->request->get('apiversion') == 'v2') {
                //2017-02-16 v2版本
                $apiReadyRecharge = Wallet::apiReadyRechargeByV2Version($userId, $money, $payType);
                if($this->request->has('iOSorAndroid')) {
                    $userAgent = $this->request->get('iOSorAndroid');
                } else {
                    $userAgent = $this->request->header('user-agent');
                }
                $ip = $this->request->ip();
                StatisticManagement::addUserLoginAfterInfo($userId, 'recharge', $userAgent, $ip);
            } else {
                return $this->responseError('版本信息不对');
            }
        }
        if ($apiReadyRecharge['status'] == 'failed') {
            return $this->responseError($apiReadyRecharge['message']);
        }
        $data = $apiReadyRecharge['data'];
        if(isset($data['ordernumber'])) {
            //添加地推
            SalesmanManagement::addUserSalesmanByRecharge($userId, $data['ordernumber'], $money);
        }
        return $this->responseSuccessData($data);
    }
    //充值分享修改状态
    public function updateRechargeDoubleTimeAfterShare() {
        $userId = $this->userId;
        $orderNumber = $this->request->get('ordernumber');
        $result = Wallet::updateDoubleTimeAfterShare($userId, $orderNumber);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //默认
    public function selectRechargePlatform() {
        $data = [
            [
                'name'=>'zhifubao'
            ]
            ,
            [
                'name' => 'weixin'
            ]
        ];
        return $this->responseSuccessData($data);
    }
}